<template>
  <div class="product">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-form-item>
        <el-input placeholder="产品名称" />
      </el-form-item>

      <el-form-item>
        <el-input placeholder="所属分类" />
      </el-form-item>

      <el-form-item label="添加时间">
        <el-date-picker
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <el-form-item>
        <el-date-picker
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table size="small" :data="products">
      <el-table-column label="编号" type="index" align="center" :index="1"></el-table-column>
      <el-table-column label="图片" width="80px">
        <template v-slot="scope">
          <img
            style="width:50px;height:50px;border-radius:5px;"
            :src="'http://121.199.29.84:8888/'+scope.row.photo"
          />
        </template>
      </el-table-column>
      <el-table-column label="名称" prop="name" width="120px"></el-table-column>
      <el-table-column label="单价" prop="price" width="60px"></el-table-column>
      <el-table-column label="所属分类" prop="category.name" width="120px"></el-table-column>
      <el-table-column label="介绍" prop="introduce"></el-table-column>
      <el-table-column label="操作" width="160px" align="center">
        <template v-slot="scope">
          <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
          <el-button type="primary" size="mini" @click="edit(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="get"
    />
    <!-- /表格 -->
    <!-- 模态框 -->
    <el-dialog title="录入产品信息" :visible.sync="visible" width="50%">
      <el-form label-width="80px">
        <el-form-item label="产品名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="所属栏目">
          <!-- <el-select v-model="form.categoryId">
            <el-option v-for="c in categories" :key="c.id" :label="c.name" :value="c.id"></el-option>
          </el-select>-->

          <el-cascader
            v-model="form.categoryId"
            :options="categories"
            :props="{label:'name',value:'id',checkStrictly: true ,emitPath:false }"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="产品单价">
          <el-input v-model="form.price"></el-input>
        </el-form-item>
        <el-form-item label="产品介绍">
          <el-input v-model="form.introduce" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="upload-demo"
            :multiple="false"
            :limit="1"
            :on-success="uploadSuccessHandler"
            :on-error="uploadErrorHandler"
            action="http://121.199.29.84:5588/file/upload"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 模态框 -->
  </div>
</template>
<script>
import { get, post } from '@/utils/request'
export default {
  data() {
    return {
      name: '产品管理',
      visible: false,
      products: [],
      categories: [],
      form: {},
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 9 //总记录数
    }
  },
  created() {
    // 调用加载数据方法进行数据加载
    this.loadProducts()
    // 调用查询栏目的方法加载栏目数据
    this.loadCategories()
  },
  methods: {
    uploadSuccessHandler(response) {
      let photo = response.data.groupname + '/' + response.data.id
      // this.form.photo=photo;
      //...解构
      this.form = { ...this.form, photo }
    },
    uploadErrorHandler(error) {
      this.$message({ type: 'error', message: '上传失败' })
    },
    // 提交
    submit() {
      let url = 'http://47.93.12.6:8889/product/saveOrUpdate'
      post(url, this.form).then(response => {
        // 提示
        this.$message({ type: 'success', message: response.message })
        // 关闭模态框
        this.visible = false
        // 刷新数据
        this.loadProducts()
      })
    },
    toAdd() {
      // 重置form
      this.form = {}
      //打开模态框
      this.visible = true
    },
    edit(row) {
      //将当前行信息绑定
      this.form = row
      // 打开模态框
      this.visible = true
    },
    del(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除
        let url = 'http://47.93.12.6:8889/product/deleteById'
        get(url, { id: row.id }).then(response => {
          // 提示
          this.$message({ type: 'success', message: response.message })
          // 刷新数据
          this.loadProducts()
        })
      })
    },
    // 加载产品数据
    loadProducts() {
      let url = 'http://47.93.12.6:8889/product/findAllWithCategory'
      get(url).then(response => {
        this.products = response.data
      })
    },
    // 加载栏目数据
    loadCategories() {
      let url = 'http://47.93.12.6:8889/category/findAllWithChild'
      get(url).then(response => {
        this.categories = response.data
      })
    }
  }
}
</script>


